package view;

import model.Region;
import model.State;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class NewRegion extends javax.swing.JPanel {
  public static final String NAME = "region";
  private SnakesCRUD parent;
  private ArrayList<State> states;

  public NewRegion(SnakesCRUD parent) {
    this.parent = parent;
    this.states = new ArrayList<State>();
    initComponents();
    this.updateStates();
  }

  public final void updateStates() {
    try {
      ResultSet r = parent.executeQuery("SELECT * FROM STATE");
      int aux = 0;

      ((DefaultListModel) (jList1.getModel())).clear();
      states.clear();

      while (r.next()) {
        states.add(aux, new State(r.getInt("StateID"), r.getString("State_Name")));
        ((DefaultListModel) (jList1.getModel())).addElement(r.getString("State_Name"));
        aux++;
      }

      if (aux == 0) {
        JOptionPane.showMessageDialog(null, "There is no data on STATE table!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
    }
    catch (SQLException e) {}
  }
    
  private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Editing REGION Table");

        jLabel2.setText("Region ID:");

        jLabel3.setText("Description:");

        jLabel4.setText("State ID:");

        jList1.setModel(new DefaultListModel());

        jScrollPane1.setViewportView(jList1);

        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Remove");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Modify");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Refresh");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(35, 35, 35)
                                .addComponent(jLabel4))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3)))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(59, 59, 59))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                            .addComponent(jLabel4))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addGap(23, 23, 23))
        );
    }// </editor-fold>//GEN-END:initComponents

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    if (!jTextField1.getText().isEmpty() && !jTextField2.getText().isEmpty() && jList1.getSelectedIndex() != -1) {
      try {
        Region reg = new Region(Integer.parseInt(this.jTextField1.getText()), states.get(jList1.getSelectedIndex()).getId(), jTextField2.getText());

        parent.executeCommand(reg.toInsert());
        JOptionPane.showMessageDialog(null, "Data added!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    if (!jTextField1.getText().isEmpty()) {
      try {
        Region reg = new Region(Integer.parseInt(this.jTextField1.getText()), states.get(jList1.getSelectedIndex()).getId(), jTextField2.getText());

        parent.executeCommand(reg.toDelete());
        JOptionPane.showMessageDialog(null, "Data deleted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }

  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    if (!jTextField1.getText().isEmpty() && !jTextField2.getText().isEmpty() && jList1.getSelectedIndex() != -1) {
      try {
        Region reg = new Region(Integer.parseInt(this.jTextField1.getText()), states.get(jList1.getSelectedIndex()).getId(), jTextField2.getText());

        parent.executeCommand(reg.toUpdate());
        JOptionPane.showMessageDialog(null, "Data updated!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }

  private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    updateStates();
  }
  
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JButton jButton4;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JList jList1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextField jTextField1;
  private javax.swing.JTextField jTextField2;
}
